package org.qydjk.console.controller.company;

import org.elasticsearch.common.collect.HppcMaps;
import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.company.InviteOffer;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.console.service.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/invite")
public class InviteController extends BaseController{
    @Autowired
    private ICompanyService companyService;

    @RequestMapping("/gotoPage.do")
    public String gotoPage(){
        return "company/invite";
    }

    /**
     * 招标列表
     */
    @RequestMapping("/invitedg.do")
    @ResponseBody
    public Object invitedg(Integer page,Integer rows,String compName,Integer status){
        HibernatePage pager = new HibernatePage(page,rows);
        List<Map<String,Object>> productList = new ArrayList<Map<String, Object>>();
        String sql="select c.name as companyName,i.*,s.name from qy_invite_record i " +
                "left join qy_company c on i.companyId = c.id " +
                "left join qy_seller s on s.id = i.sellerId " +
                "where 1=1 " ;
        String countSql="select count(c.id) from qy_invite_record i " +
                "left join qy_company c on i.companyId = c.id " +
                "where 1=1 ";
        if(!StringUtil.isEmptyOrNull(compName)){
            sql+="and c.name like '%"+compName+"%'";
            countSql+="and c.name like '%"+compName+"%'";
        }
        sql+="  ORDER BY  i.publish_time desc";
        try {
            productList=baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total,productList);
        } catch (Exception e) {
            e.printStackTrace();
            return new EasyUIResponse(0,productList);
        }
    }
    /**
     * 报价列表
     */
    @RequestMapping("/offerdg.do")
    @ResponseBody
    public Object offerdg(Integer page,Integer rows,Integer inviteId){
        HibernatePage pager = new HibernatePage(page,rows);
        String sql=" select o.*,s.name from qy_invite_offer o " +
                "left join qy_seller s on o.seller_id = s.id ";
        String countSql="select count(*) from qy_invite_offer ";
        if(inviteId!=null){
            sql+="where o.invite_id="+inviteId;
            countSql+="where invite_id="+inviteId;
        }
        sql+=" ORDER BY  o.offer_time desc";
        try {
            List<Map<String,Object>> productList=baseService.findMapBySQL(sql,pager);
            int total = baseService.getRecordCountSQL(countSql);
            return new EasyUIResponse(total,productList);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 详情
     */
    @RequestMapping("/getDetail.do")
    @ResponseBody
    public Object getDetail(Integer invteId) throws Exception {
        try{
            String sql="select single_category_id as id,name from qy_invite_single_category where invite="+invteId ;
            List<Map<String,Object>> categoryList = baseService.findMapBySQL(sql);
            List<Map<String,Object>> resList = new ArrayList<Map<String, Object>>();
            for(Map catemap :categoryList){
                String singsql="select * from qy_invite_single where inviteId="+invteId+" and single_project_category_id ="+catemap.get("id") ;
                List<Map<String,Object>> singleList = baseService.findMapBySQL(singsql);
                if(singleList.size()>0){
                    Map<String,Object> ss = new HashMap<String,Object>();
                    ss.put(catemap.get("name").toString(),singleList);
                    resList.add(ss);
                }
            }
            return resList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*撤销发布*/
    @RequestMapping("/cancelPush.do")
    @ResponseBody
    public Object cancelPush(Integer productId){
        try {
            companyService.updateInvite(productId);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("系统错误，请联系管理员");
        }
    }

        /*撤销报价*/
    @RequestMapping("/cancelOffer.do")
    @ResponseBody
    public Object cancelOffer(Integer offerId){
        try {
            InviteOffer offer = baseService.get(InviteOffer.class,offerId);
            offer.setStatus(1);
            baseService.saveOrUpdate(offer);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("系统错误，请联系管理员");
        }
    }

    /*确认报价*/
    @RequestMapping("/okOffer.do")
    @ResponseBody
    public Object okOffer(Integer offerId){
        try {
            companyService.updateOffer(offerId);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("系统错误，请联系管理员");
        }
    }
}
